package com.example.config;

import jdk.nashorn.internal.ir.CallNode;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @PackageName:com.example.config
 * @className:MyLocaleResolver
 * @Description: 国际化处理
 * @author:朱楚利
 * @date:2021/5/11 9:09
 */
@Configuration
public class MyLocaleResolver implements LocaleResolver {


    /***
     * @Description 国际化设置操作
     * @Date 9:16 2021/5/11
     * @author 朱楚利
    */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取请求中的请求参数
        String language  = request.getParameter("l");
        Locale locale = request.getLocale(); //如果没有就使用默认的
        //如果请求的链接携带了国际化参数
        if(!StringUtils.isEmpty(language)){
            //zh_CN
            String[] s = language.split("_");
            //国家，地区
            Locale locale1 = new Locale(s[0], s[1]);
            return locale1;
        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
